/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workflowinterface;

import java.awt.Component;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author Vinícius
 */
public class PathTreeRenderer extends DefaultTreeCellRenderer
{
    private ImageIcon fileIcon;
    private ImageIcon directoryIcon;

    public PathTreeRenderer(ImageIcon file,ImageIcon directory)
    {
        super();
        fileIcon = file;
        directoryIcon = directory;
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree,Object value,
                        boolean sel,boolean expanded,boolean leaf,int row,
                        boolean hasFocus)
    {
        super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        if(node.getUserObject().getClass() == File.class)
        {
            File f = (File)node.getUserObject();
	    int index = f.getName().lastIndexOf(".");
	    String name = index >= 0 ? f.getName().substring(0,index) : f.getName();
            this.setText(name);
            if(leaf && f.isFile())
                setIcon(fileIcon);
            else
                setIcon(directoryIcon);
        }
        return this;
    }
}